// 异步任务的串行流程控制 原理实现

// 任务数组
const tasks = [task_1, task_2, task_3, task_4] 

function next(err, res) {
    if (err) throw err
    const task = tasks.shift()
    if (task) {
        console.log(res)
        task(res)
    }
}

function task_1() {
    setTimeout(() => {
        console.log('task 1');
        next(null, 3000)
    }, 3000)
}

function task_2() {
    setTimeout(() => {
        console.log('task 2');
        next(null, 1000)
    }, 1000)
}

function task_3() {
    setTimeout(() => {
        console.log('task 3');
        next(null, 4000)
    }, 4000)
}

function task_4() {
    setTimeout(() => {
        console.log('task 4');
        next(null, 100)
    }, 100)
}

next(null, 'start')